﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;

using CE.Provider;
public partial class Provider_UploadPolicyDoc : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] != null && Session[CE.Global.General.UserTypeId] != null)
        {
            if (Convert.ToInt32(Session[CE.Global.General.UserTypeId]) !=
                Convert.ToInt32(CE.Global.CEUserType.Provider))
            {
                Response.Redirect("ProviderSecureLogin.aspx");
            }
        }
        else
        {
            Response.Redirect("ProviderSecureLogin.aspx");
        }

        if (Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("ProviderSecureLogin.aspx");
        }
    }

    private static bool CheckExtension(string Extension)
    {
        string[] extList = { ".doc", ".pdf", ".docx", ".txt", ".rtf", ".ppt", ".pptx", ".xls" };
        bool isInList = false;
        for (int i = 0; i < extList.Length; i++)
        {
            if (Extension == extList[i])
            {
                isInList = true;
                break;
            }
        }
        return isInList;
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (fupDocument.HasFile)
        {
            int _sizekb = 0;
            _sizekb = fupDocument.PostedFile.ContentLength / 1024;
            if (_sizekb > 4096)
            {
                lblFileUploadMessage.Text = "File size is bigger than 4 MB.";
                return;
            }
            string fname = fupDocument.PostedFile.FileName;
            FileInfo fInfo = new FileInfo(fname);

            if (CheckExtension(fInfo.Extension) == true)
            {
                string _strPath = CE.Configuration.MyConfigurationManager.GetAppSettingValue("UploadDocPath");
                fupDocument.SaveAs(Server.MapPath(_strPath + fInfo.Name));
                ProviderPolicyDocument _document = new ProviderPolicyDocument();
                ProviderPolicyDocumentDB _documentdb = new ProviderPolicyDocumentDB();
                _document.DocumentName = Server.HtmlEncode(txtDocumentName.Text);
                _document.Description = Server.HtmlEncode(txtDescription.Text);
                _document.ProviderCompanyId = Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString());
                _document.SizeKB = _sizekb;
                _document.RefCEUserId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());
                if (_documentdb.Insert(_document) > 0)
                {
                    lblFileUploadMessage.Text = "Uploaded successfully!";
                }
                else
                {
                    lblFileUploadMessage.Text = "Uploaded failed!";
                }
                //myResume.IsFileUploadComplete = true;
                //myResume.ResumeFileUploadPath = fInfo.FullName;
            }
            else
            {
                lblFileUploadMessage.Text = "File type is not allowed!";
                return;
            }
        }
    }
}
